import 'package:flutter/cupertino.dart';
import 'package:flutter_test1/db/db.dart';
import 'package:sqflite/sqflite.dart';

abstract class BaseTable {
  /// 表名
  String get tableName;

  /// 创建表的 SQL
  String get createTableSQL;

  /// 初始化表
  Future<void> createTable(Database db) async {
    await db.execute(createTableSQL);
  }

  /// 插入数据
  Future<int> insert(Map<String, dynamic> values) async {
    final db = await DbManager().database;
    debugPrint('插入数据：$values');
    return db.insert(tableName, values);
  }

  /// 查询数据
  Future<List<Map<String, dynamic>>> query({String? where, List<Object?>? whereArgs, String? orderBy}) async {
    final db = await DbManager().database;
    return db.query(tableName, where: where, whereArgs: whereArgs, orderBy: orderBy);
  }

  /// 更新数据
  Future<int> update(Map<String, dynamic> values, {String? where, List<Object?>? whereArgs}) async {
    final db = await DbManager().database;
    return db.update(tableName, values, where: where, whereArgs: whereArgs);
  }

  /// 删除数据
  Future<int> delete({String? where, List<Object?>? whereArgs}) async {
    final db = await DbManager().database;
    return db.delete(tableName, where: where, whereArgs: whereArgs);
  }
}
